package com.huicoo.forestmanager.util.audio;

import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;

import com.huicoo.forestmanager.R;

public class PlayAudioRecordDialog extends Dialog implements View.OnClickListener {
    private MediaPlayer mMediaPlayer;
    private OnPlayVoiceErrorListener onPlayVoiceErrorListener;

    public PlayAudioRecordDialog(Context context) {
        super(context, R.style.VoiceDialog);
        setContentView(R.layout.dialog_play_audio_record);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();

    }

    private void initView() {
        View close = findViewById(R.id.close);
        close.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                stopPlayMusic();
                dismiss();
                break;
            default:
                break;
        }
    }

    //开始播放录音
    private void startPlayMusic(String filePath) {
        if (mMediaPlayer != null) {
            stopPlayMusic();
        }
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(filePath);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    // 装载完毕回调
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlayMusic();
                    dismiss();
                }
            });
            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    stopPlayMusic();
                    if (onPlayVoiceErrorListener != null) {
                        onPlayVoiceErrorListener.onError();
                    }
                    dismiss();
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //结束录音播放
    private void stopPlayMusic() {
        if (mMediaPlayer == null)
            return;
        try {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public void showDialog(String filePath, OnPlayVoiceErrorListener onPlayVoiceErrorListener) {
        show();
        startPlayMusic(filePath);
        this.onPlayVoiceErrorListener = onPlayVoiceErrorListener;
    }

    public interface OnPlayVoiceErrorListener {
        void onError();
    }
}
